package com.zyw.zhuangdian.controller;

import com.alibaba.fastjson.JSONObject;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.in.ApproveDtoIn;
import com.zyw.zhuangdian.pojo.out.ApproveDtoOut;
import com.zyw.zhuangdian.service.ApproveService;
import com.zyw.zhuangdian.util.Assert;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import com.zyw.zhuangdian.util.RespUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/approve")
@RequiredArgsConstructor
@Slf4j
public class ApproveController {
    private final ApproveService approveService;

    /**
     * 新增文章
     * @param json
     * @return
     */
    @RequestMapping(value = "doApprove",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> addApprove(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"fromUid","点赞者id不能为空");
        Assert.hasKeyAndValue(json,"toAid","点赞内容id不能为空");
        Assert.hasKeyAndValue(json,"type","文章类型不能为空");

        ApproveDtoIn in = BeanConvertUtil.covertBean(json, ApproveDtoIn.class);
        Resp<Void> resp = approveService.addApprove(in);
        return RespUtil.OK(resp);
    }


    /**
     * 删除文章
     * @param json aid
     * @return
     */
    @RequestMapping(value = "cancelApprove",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> deleteApprove(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"fromUid","点赞者id不能为空");
        Assert.hasKeyAndValue(json,"toAid","点赞内容id不能为空");
        Assert.hasKeyAndValue(json,"type","文章类型不能为空");

        ApproveDtoIn in = BeanConvertUtil.covertBean(json, ApproveDtoIn.class);
        Resp<Void> resp = approveService.deleteApprove(in);
        return RespUtil.OK(resp);
    }

    /**
     * 列出文章
     * @param json
     * @return
     */
    @RequestMapping(value = "listApprove",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> listApprove(@RequestBody JSONObject json){
        ApproveDtoIn in = BeanConvertUtil.covertBean(json, ApproveDtoIn.class);
        Resp<List<ApproveDtoOut>> resp = approveService.listApproves(in);
        return RespUtil.OK(resp);
    }
}
